﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;
using System.IO;
using System.Web;
using System.Web.Script.Serialization;

namespace Comet
{
    class Verify
    {
        public static string app_id = "213199302051720";
        public static string app_secret = "86bf09cf3049fcee5642f9718cf75f86";
        public static string url = "http://zuchat.com";
        public static string verification_url = url + "/verify";

        public static string Permissions = "";
        static JavaScriptSerializer json = new JavaScriptSerializer();
        public static string cookieExpireData = "; Max-Age=31622400";

        public static void Handle(HttpListenerContext context, string requestParams)
        {
            try
            {
                HttpListenerResponse replyResponse = context.Response;
                Dictionary<string, string> paramsDict = Extra.ParseRawParams(requestParams);

                if (paramsDict.ContainsKey("chatwith") && paramsDict["chatwith"] == "anyone")
                {
                    replyResponse.Headers.Add("Set-Cookie", "showhome=false" + cookieExpireData);
                    replyResponse.Headers.Add("Set-Cookie", "gender=anyone" + cookieExpireData);
                    replyResponse.Headers.Add("Set-Cookie", "chatwith=anyone" + cookieExpireData);
                    replyResponse.Headers.Add("Location", "/");
                    replyResponse.StatusCode = (int)HttpStatusCode.Redirect;

                }
                else if (!paramsDict.ContainsKey("state"))
                {
                    string facebookUrl = "http://www.facebook.com/dialog/oauth?scope="
                        + Permissions
                        + "&client_id=" + app_id
                        + "&redirect_uri=" + Uri.EscapeUriString(verification_url + "?chatwith=" + paramsDict["chatwith"])
                        + "&state=" + Extra.GenerateChatID;
                    replyResponse.Headers.Add("Set-Cookie", "explain=true" + cookieExpireData);
                    replyResponse.Headers.Add("Set-Cookie", "requestedchatwith=" + paramsDict["chatwith"] + cookieExpireData);
                    replyResponse.Headers.Add("Location", facebookUrl);
                    replyResponse.StatusCode = (int)HttpStatusCode.Redirect;
                }
                else if (!paramsDict.ContainsKey("error_reason"))
                {
                    string token_url = "https://graph.facebook.com/oauth/access_token?"
                        + "client_id=" + app_id
                        + "&redirect_uri=" + Uri.EscapeUriString(verification_url + "?chatwith=" + paramsDict["chatwith"])
                        + "&client_secret=" + app_secret
                        + "&code=" + paramsDict["code"];

                    string response = new WebClient().DownloadString(token_url);
                    Dictionary<string, string> responceParams = Extra.ParseRawParams(response);
                    string graph_url = "https://graph.facebook.com/me?access_token=" + responceParams["access_token"];
                    string graphData = new WebClient().DownloadString(graph_url);
                    var user = json.Deserialize<Dictionary<string, object>>(graphData);
                    string gender = user["gender"].ToString();
                    //string name = user["name"].ToString();
                    //string id = user["id"].ToString();
                    //File.WriteAllText(id + ".txt", graphData);

                    string requestedchatwith = "anyone";
                    try { requestedchatwith = paramsDict["chatwith"]; }
                    catch { }
                    replyResponse.Headers.Add("Set-Cookie", "showhome=false" + cookieExpireData);
                    replyResponse.Headers.Add("Set-Cookie", "gender=" + gender + cookieExpireData);
                    replyResponse.Headers.Add("Set-Cookie", "chatwith=" + requestedchatwith + cookieExpireData);
                    replyResponse.Headers.Add("Set-Cookie", "explain=false" + cookieExpireData);
                    replyResponse.Headers.Add("Location", "/");
                    replyResponse.StatusCode = (int)HttpStatusCode.Redirect;
                }
                else
                {
                    replyResponse.Headers.Add("Set-Cookie", "explain=true" + cookieExpireData);
                    replyResponse.Headers.Add("Location", "/?chatwith=" + paramsDict["chatwith"]);
                    replyResponse.StatusCode = (int)HttpStatusCode.Redirect;
                }

                Communications.SendResponse(context, Communications.DataType.Text, "0");
            }
            catch { Communications.ReplyCompleted(context); }
        }

    }
}
